<template>
  <el-dialog :visible="showDialog" title="新增员工" top="8vh" @close="close">
    <!-- 表单 -->
    <el-form ref="addForm" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" style="width:50%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" style="width:50%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" style="width:50%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" style="width:50%" placeholder="请选择">
          <el-option
            v-for="item in EmployeeEnum.hireType"
            :key="item.id"
            :value="item.id"
            :label="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" style="width:50%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="formData.departmentName" style="width:50%" placeholder="请选择部门" @click.native.stop="showTree" />
        <div v-if="isShowTree" class="tree-box">
          <el-tree
            v-loading="treeLoading"
            :data="treeList"
            default-expand-all
            :props="{label:'name'}"
            @node-click="selectNode"
          />
        </div>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="formData.correctionTime" style="width:50%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-button @click="close">取消</el-button>
      <el-button type="primary" @click="submit">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>

import EmployeeEnum from '@/api/constant/employees.js'
import { addEmployeeAPI } from '@/api'
import { transToTree } from '@/utils'
import { getDepartment } from '@/api'
export default {
  props: {
    showDialog: { // 控制弹窗显示隐藏
      type: Boolean,
      default: false

    }
  },
  data() {
    return {
      EmployeeEnum, // 引入的枚举
      // 提交的表单
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      // 验证的表单规则
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: ['blur', 'change'] }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: ['blur', 'change'] },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: ['blur', 'change'] }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: ['blur', 'change'] }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: ['blur', 'change'] }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: ['blur', 'change'] }
        ]
      },

      treeList: [], // 部门树的数据
      isShowTree: false, // 这个标签选项是否隐藏
      treeLoading: false // 加载动画定义
    }
  },
  methods: {
    // 弹窗关闭触发的函数
    close() {
      this.$refs.addForm.resetFields() // 清空验证
      this.formData = {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      }// 清空表单
      // 告父亲关闭弹窗
      this.$emit('close')
      this.isShowTree = false // 隐藏
    },
    // 点击部门触发事件 显示树形
    async showTree() {
      this.treeLoading = true // 打开加载动画
      const { data } = await getDepartment()
      this.treeList = transToTree(data.depts, '') // 变换树形
      this.isShowTree = true // 这个标签显示
      this.treeLoading = false // 关闭加载动画
    },
    // 声明树的点击事件
    selectNode(val) {
      // 判断有没有孩子
      if (val.children && val.children.leng > 0) {
        return
      }
      this.formData.departmentName = val.name// 把表单名字赋值给数据 实现双向数据绑定
      this.isShowTree = false // 隐藏含树形的标签
    },
    // 确认提交
    submit() {
      // 兜底验证
      this.$refs.addForm.validate(async(valid) => {
        if (valid) {
          const res = await addEmployeeAPI(this.formData)
          // console.log(res)
          this.$message.success(res.message) // 信息提示

          this.close() // 关闭弹窗

          this.$emit('add')// 通知父组件重新铺设数据
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.tree-box {
  position: absolute;
  width: 50%;
  min-height: 50px;
  left: 0;
  top: 45px;
  z-index: 100;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding-right: 5px;
  overflow: hidden;
  background-color: #fff;
  max-height: 200px;
  overflow: auto;
  ::v-deep {
    .el-tree-node__content {
      height: auto;
    }
  }
}
</style>
